define(function (require, exports, module) {
    var $ = require("jquery");
    require("pagination");
    var util = require("util");
    var ui = require("ui");
    var angular = require("angular");
    var api_prefix = G_CONFIG["api_prefix"] + 'qmyx'; //api前缀

    var ditchSourceModule = angular.module('ditchSourceModule', []);

    function ditchSourceService($http, $q, $location) {
        var service = {
            ditchSources: [],//ditchSources列表
            filter: {pageSize: 10},

            initData: function () {
                var p = service.filter;
                service.query(p.pageIndex || 1, p.pageSize || 10, p.keyword || '');
            },
            query: function (pageIndex, pageSize, keyword) {
                service.filter = {pageIndex: pageIndex, pageSize: pageSize, keyword: keyword};
                $http.get(api_prefix + '/channel-source/get-channel-source-list', {params: service.filter})
                    .then(function (res) {
                        var retCode = res.data.retCode;
                        if (retCode == 0) {
                            var data = res.data.data;
                            service.ditchSources = data.items;
                            service.total = data.total;
                            service.filter.pageIndex = pageIndex;
                        } else {
                            ui.showErrTip(res.data.errMsg);
                            return false;
                        }
                    });
            },
            // 初始化查询表单数据
            initEditSourceDialog: function ($data, $dialog, $dialogScope, ditchsource, baseSvr) {
                $dialogScope.baseSvr = baseSvr;
                $dialogScope.setFontColor = function (color) {
                    $data.font_color = color;
                    //为了兼容万恶的IE
                    $('.fontcolor-picker').css({'background': color});
                    $('.fontcolor-tmpl').css({'color': color});
                }
                $dialogScope.setBtnColor = function (color) {
                    $data.button_color = color;
                    //为了兼容万恶的IE
                    $('.buttoncolor-picker').css({'background': color});
                    $('.buttoncolor-tmpl').css({'background': color});
                }
                $(document).on('click', function (e) {
                    if (!$(e.target).closest('.font-board,.color-picker').length) {
                        $dialogScope.showFontColorPicker = false;
                        $dialogScope.showBtnColorPicker = false;
                        $dialogScope.$applyAsync();
                    }
                });

                angular.extend($data, ditchsource);
                //为了兼容万恶的IE
                //初始化按钮、字的颜色
                $('.fontcolor-picker').css({'background': ditchsource['font_color']});
                $('.fontcolor-tmpl').css({'color': ditchsource['font_color']});
                $('.buttoncolor-picker').css({'background': ditchsource['button_color']});
                $('.buttoncolor-tmpl').css({'background': ditchsource['button_color']});

                //上传二维码内部图片
                $dialogScope.uploadQRImages = function (status) {
                    switch (status.status) {
                        case 'error':
                            if (status.msg.toString().match(/File\s+size\s+error/)) {
                                $dialogScope.uploader.stateqr = '图片大小超出限制';
                            } else if (status.msg.toString().match(/File\s+extension\s+error/)) {
                                $dialogScope.uploader.stateqr = '图片类型不符';
                            }
                            else {
                                $dialogScope.uploader.stateqr = status.msg;
                            }

                            break;
                        case 'success':
                            //获取二维码图片
                            setTimeout(function () {
                                var parms = {qr_code_innner_img: $data.qr_code_innner_img};
                                $http.get(api_prefix + '/channel-source/crete-qr-code-img-url', {params: parms})
                                    .then(function (res) {
                                        var retCode = res.data.retCode;
                                        if (retCode == 0) {
                                            $data.qr_code_img = res.data.data;
                                        } else {
                                            ui.showErrTip(res.data.errMsg);
                                            return false;
                                        }
                                    });
                            }, 10);

                            break;
                    }
                }

                $dialogScope.uploadBackImages = function (status) {
                    switch (status.status) {
                        case 'error':
                            if (status.msg.toString().match(/File\s+size\s+error/)) {
                                $dialogScope.uploader.state = '图片大小超出限制';
                            } else if (status.msg.toString().match(/File\s+extension\s+error/)) {
                                $dialogScope.uploader.state = '图片类型不符';
                            }
                            else {
                                $dialogScope.uploader.state = status.msg;
                            }

                            break;
                    }
                }

                $dialogScope.uploadThumbnailImages = function (status) {
                    switch (status.status) {
                        case 'error':
                            if (status.msg.toString().match(/File\s+size\s+error/)) {
                                $dialogScope.uploader.state2 = '图片大小超出限制';
                            } else if (status.msg.toString().match(/File\s+extension\s+error/)) {
                                $dialogScope.uploader.state2 = '图片类型不符';
                            }
                            else {
                                $dialogScope.uploader.state2 = status.msg;
                            }

                            break;
                    }
                }

                $dialogScope.colors = [
                    {value:'#ffffff',text:'白色'},
                    {value:'#000000',text:'黑色'},
                    {value:'#ff9f16',text:'桔色'},
                    {value:'#f06387',text:'猩红'},
                    {value:'#da6ce4',text:'酱紫'},
                    {value:'#8b6af6',text:'紫色'},
                    {value:'#5e58de',text:'蓝紫'},
                    {value:'#0974cf',text:'海蓝'},
                    {value:'#0eccca',text:'亮蓝'},
                    {value:'#75b93e',text:'草绿'},
                    {value:'#f7691c',text:'橙黄'},
                    {value:'#0bafe5',text:'天蓝'},
                    {text: '淡紫红',value:'#FFF0F5'},
                    {text: '浅粉红',value:'#FFB6C1'},
                    {text: '粉红',value:'#FFC0CB'},
                    {text: '热情的粉红',value:'#FF69B4'},
                    {text: '弱紫罗兰红',value:'#DB7093'},
                    {text: '深粉红',value:'#FF1493'},
                    {text: '中紫罗兰红',value:'#C71585'},
                    {text: '深红(猩红)',value:'#DC143C'},
                    {text: '淡紫色(熏衣草淡紫)',value:'#E6E6FA'},
                    {text: '蓟色',value:'#D8BFD8'},
                    {text: '洋李色(李子紫)',value:'#DDA0DD'},
                    {text: '紫罗兰',value:'#EE82EE'},
                    {text: '暗紫色(兰花紫)',value:'#DA70D6'},
                    {text: '洋红(玫瑰红)',value:'#FF00FF'},
                    {text: '紫红(灯笼海棠)',value:'#FF00FF'},
                    {text: '中兰花紫',value:'#BA55D3'},
                    {text: '中紫色',value:'#9370DB'},
                    {text: '蓝紫罗兰',value:'#8A2BE2'},
                    {text: '暗紫罗兰',value:'#9400D3'},
                    {text: '暗兰花紫',value:'#9932CC'},
                    {text: '深洋红',value:'#8B008B'},
                    {text: '紫色',value:'#800080'},
                    {text: '靛青/紫兰色',value:'#4B0082'},
                    {text: '爱丽丝蓝',value:'#F0F8FF'},
                    {text: '蔚蓝色',value:'#F0FFFF'},
                    {text: '亮蓝',value:'#ADD8E6'},
                    {text: '粉蓝色(火药青)',value:'#B0E0E6'},
                    {text: '亮天蓝色',value:'#87CEFA'},
                    {text: '天蓝色',value:'#87CEEB'},
                    {text: '深天蓝',value:'#00BFFF'},
                    {text: '矢车菊蓝',value:'#6495ED'},
                    {text: '闪兰色(道奇蓝)',value:'#1E90FF'},
                    {text: '皇家蓝/宝蓝',value:'#4169E1'},
                    {text: '亮钢蓝',value:'#B0C4DE'},
                    {text: '军兰色(军服蓝)',value:'#5F9EA0'},
                    {text: '钢蓝/铁青',value:'#4682B4'},
                    {text: '亮蓝灰(亮石板灰)',value:'#778899'},
                    {text: '灰石色(石板灰)',value:'#708090'},
                    {text: '中暗蓝色(中板岩蓝)',value:'#7B68EE'},
                    {text: '石蓝色(板岩蓝)',value:'#6A5ACD'},
                    {text: '暗灰蓝色(暗板岩蓝)',value:'#483D8B'},
                    {text: '纯蓝',value:'#0000FF'},
                    {text: '中蓝色',value:'#0000CD'},
                    {text: '午夜蓝',value:'#191970'},
                    {text: '暗蓝色',value:'#00008B'},
                    {text: '海军蓝',value:'#000080'},
                    {text: '淡青色',value:'#E0FFFF'},
                    {text: '青色',value:'#00FFFF'},
                    {text: '暗瓦灰色(暗石板灰)',value:'#2F4F4F'},
                    {text: '暗青色',value:'#008B8B'},
                    {text: '水鸭色',value:'#008080'},
                    {text: '弱绿宝石',value:'#AFEEEE'},
                    {text: '浅绿色(水色)',value:'#00FFFF'},
                    {text: '宝石碧绿',value:'#7FFFD4'},
                    {text: '中宝石碧绿',value:'#66CDAA'},
                    {text: '绿宝石',value:'#40E0D0'},
                    {text: '中绿宝石',value:'#48D1CC'},
                    {text: '暗绿宝石',value:'#00CED1'},
                    {text: '淡绿色',value:'#90EE90'},
                    {text: '弱绿色',value:'#98FB98'},
                    {text: '中春绿色',value:'#00FA9A'},
                    {text: '春绿色',value:'#00FF7F'},
                    {text: '浅海洋绿',value:'#20B2AA'},
                    {text: '海洋绿',value:'#2E8B57'},
                    {text: '中海洋绿',value:'#3CB371'},
                    {text: '暗海洋绿',value:'#8FBC8F'},
                    {text: '森林绿',value:'#228B22'},
                    {text: '纯绿',value:'#008000'},
                    {text: '暗绿色',value:'#006400'},
                    {text: '闪光绿',value:'#00FF00'},
                    {text: '闪光深绿',value:'#32CD32'},
                    {text: '草绿色(草坪绿)',value:'#7CFC00'},
                    {text: '黄绿色(查特酒绿)',value:'#7FFF00'},
                    {text: '绿黄色',value:'#ADFF2F'},
                    {text: '黄绿色',value:'#9ACD32'},
                    {text: '浅黄色',value:'#FFFFE0'},
                    {text: '玉米丝色',value:'#FFF8DC'},
                    {text: '米色/灰棕色',value:'#F5F5DC'},
                    {text: '亮菊黄',value:'#FAFAD2'},
                    {text: '老花色(旧蕾丝)',value:'#FDF5E6'},
                    {text: '亚麻布',value:'#FAF0E6'},
                    {text: '柠檬绸',value:'#FFFACD'},
                    {text: '番木色(番木瓜)',value:'#FFEFD5'},
                    {text: '白杏色',value:'#FFEBCD'},
                    {text: '陶坯黄',value:'#FFE4C4'},
                    {text: '浅黄色(小麦色)',value:'#F5DEB3'},
                    {text: '鹿皮色(鹿皮靴)',value:'#FFE4B5'},
                    {text: '纳瓦白(土著白)',value:'#FFDEAD'},
                    {text: '灰菊黄(苍麒麟色)',value:'#EEE8AA'},
                    {text: '黄褐色(卡叽布)',value:'#F0E68C'},
                    {text: '暗黄褐色(深卡叽布)',value:'#BDB76B'},
                    {text: '纯黄',value:'#FFFF00'},
                    {text: '金色',value:'#FFD700'},
                    {text: '金菊黄',value:'#DAA520'},
                    {text: '暗金菊黄',value:'#B8860B'},
                    {text: '橄榄',value:'#808000'},
                    {text: '橄榄褐色',value:'#6B8E23'},
                    {text: '暗橄榄绿',value:'#556B2F'},
                    {text: '橙色',value:'#FFA500'},
                    {text: '茶色',value:'#D2B48C'},
                    {text: '硬木色',value:'#DEB887'},
                    {text: '沙棕色',value:'#F4A460'},
                    {text: '巧克力色',value:'#D2691E'},
                    {text: '秘鲁色',value:'#CD853F'},
                    {text: '重褐色(马鞍棕色)',value:'#8B4513'},
                    {text: '黄土赭色',value:'#A0522D'},
                    {text: '浅玫瑰色(薄雾玫瑰)',value:'#FFE4E1'},
                    {text: '桃肉色',value:'#FFDAB9'},
                    {text: '浅鲑鱼肉色',value:'#FFA07A'},
                    {text: '珊瑚',value:'#FF7F50'},
                    {text: '深橙色',value:'#FF8C00'},
                    {text: '淡珊瑚色',value:'#F08080'},
                    {text: '玫瑰棕色',value:'#BC8F8F'},
                    {text: '印度红',value:'#CD5C5C'},
                    {text: '鲜肉/鲑鱼色',value:'#FA8072'},
                    {text: '深鲜肉/鲑鱼色',value:'#E9967A'},
                    {text: '番茄红',value:'#FF6347'},
                    {text: '橙红色',value:'#FF4500'},
                    {text: '纯红',value:'#FF0000'},
                    {text: '棕色',value:'#A52A2A'},
                    {text: '火砖色(耐火砖)',value:'#B22222'},
                    {text: '深红色',value:'#8B0000'},
                    {text: '栗色',value:'#800000'},
                    {text: '纯白',value:'#FFFFFF'},
                    {text: '雪白色',value:'#FFFAFA'},
                    {text: '花的白色',value:'#FFFAF0'},
                    {text: '象牙色',value:'#FFFFF0'},
                    {text: '海贝壳',value:'#FFF5EE'},
                    {text: '薄荷奶油',value:'#F5FFFA'},
                    {text: '幽灵白',value:'#F8F8FF'},
                    {text: '蜜色(蜜瓜色)',value:'#F0FFF0'},
                    {text: '白烟',value:'#F5F5F5'},
                    {text: '古董白',value:'#FAEBD7'},
                    {text: '淡灰色(庚斯博罗灰)',value:'#DCDCDC'},
                    {text: '浅灰色',value:'#D3D3D3'},
                    {text: '银灰色',value:'#C0C0C0'},
                    {text: '深灰色',value:'#A9A9A9'},
                    {text: '灰色',value:'#808080'},
                    {text: '暗淡的灰色',value:'#696969'},
                    {text: '纯黑',value:'#000000'},
                ];
            },
            save: function ($data, $dialog) {
                var data = {modelData: angular.extend({}, $data)};
                var url = api_prefix + "/channel-source/save-channel-source";

                util.myAjax({
                    url: url,
                    type: "POST",
                    data: data,
                    context: this,
                    success: function (json) {
                        if (json.retCode == 0) {
                            service.initData();
                            ui.showSucTip();
                            $dialog.close();
                        } else {
                            ui.showErrTip(json.errMsg);
                        }
                    }
                });
                return false;
            },
            delete: function (ditchsource) {
                util.confirm('确定要删除此Source吗?', {
                    ok: function () {
                        var data = {ditchsourceId: ditchsource.ditchsource_id};
                        var url = api_prefix + "/rclc/ditchsource/delete-ditchsource-by-id";
                        util.myAjax({
                            url: url,
                            type: "POST",
                            data: data,
                            context: this,
                            success: function (json) {
                                if (json.retCode == 0) {
                                    service.initData();
                                    ui.showSucTip();
                                } else {
                                    ui.showErrTip(json.errMsg);
                                }
                            }
                        });
                    }
                });
            }
        };

        return service;
    }

    ditchSourceModule.factory('ditchSourceSvr', ['$http', '$q', '$location', ditchSourceService]);

    module.exports = ditchSourceModule;
});